package com.stop.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import com.stop.R;
import com.stop.activity.fragment.SalaDescriptionActivity;
import com.stop.activity.fragment.SalaPlayersActivity;
import com.stop.activity.fragment.SalaTabListener;
import com.stop.controller.StopController;

public class SalaActivity extends Activity{

	private String salaId = null;
	
	public void onBackPressed() {
		new SairSala().execute();
		super.onBackPressed();
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//sala recebida por parametro da lista clicada
		String sala = this.getIntent().getStringExtra("sala");
		try{
			salaId = new JSONObject(sala).getString("uid");
		}catch(JSONException e){
			e.printStackTrace();
		}
		
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		Bundle params = new Bundle();
		params.putString("sala", sala);
		
		Tab tabDescription = actionBar.newTab();
		tabDescription.setText(getResources().getString(R.string.title_tab_description));
		tabDescription.setTabListener(new SalaTabListener(this, SalaDescriptionActivity.class.getName(), params));
		
		Tab tabPlayers = actionBar.newTab();
		tabPlayers.setText(getResources().getString(R.string.title_tab_players));
		tabPlayers.setTabListener(new SalaTabListener(this, SalaPlayersActivity.class.getName(), params));
		
		actionBar.addTab(tabDescription);
		actionBar.addTab(tabPlayers);

	}
	
	class SairSala extends AsyncTask<Void, Void, Void> {
		protected Void doInBackground(Void... params) {
			try {
				StopController.getInstancia().sairSala(salaId);
			} catch (Exception e) {
				e.printStackTrace();
			}
				
			return null;
		}
	}
	
}
